package com.example.travelservlet.search.service;

import com.example.travelservlet.entity.HotSearch;
import com.example.travelservlet.search.repository.HotSearchRepository;
import com.example.travelservlet.utils.StrSimilarityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author kiki
 * @dtae 2023/4/23 11:10
 */
@Service
public class HotSearchService {
    @Autowired
    private HotSearchRepository hotSearchRepository;
    public void add(String hotTitle) {
        //获取用户搜索词条
        //判断该词条是否为2个字以上
        System.out.println("词条：" + hotTitle);
        System.out.println("词条长度：" + hotTitle.length());
        if (hotTitle.length() >= 2) {
            System.out.println("词条长度大于2");
            //数据库中的所有热搜词
            List<HotSearch> hotSearchList = hotSearchRepository.findAll();
            if (hotSearchList.isEmpty()) {
                System.out.println("数据库中没有词条");
                HotSearch newHotSearch = new HotSearch();
                newHotSearch.setHotTitle(hotTitle);
                newHotSearch.setClickNum(1);
                HotSearch save = hotSearchRepository.save(newHotSearch);
                save.toString();
            }
            //判断该词条与数据库中的所有热搜词的相似度
            Map<Integer, Float>  similarityRatioMap = new HashMap<>();
            similarityRatioMap.clear();
            hotSearchList.forEach(f ->{
                System.out.println("数据库中有词条");
                float similarityRatio = StrSimilarityUtils.getSimilarityRatio(f.getHotTitle(), hotTitle);
                System.out.println("与数据库词条"+f.getHotTitle()+"的相似度"+similarityRatio);
                similarityRatioMap.put(f.getHotSearchId(), similarityRatio);//记录新词条与全部已有词条的相似度
            });
            //找出最大相似度
            Optional<Map.Entry<Integer, Float>> maxRatio = similarityRatioMap.entrySet()
                    .stream()
                    .max(Map.Entry.comparingByValue());
            //最大相似度40%以下就将该词条加入数据库
            if (maxRatio.get().getValue() < 0.4) {
                System.out.println("新词条与数据库词条最大相似度小于0.4，新增词条");
                HotSearch newHotSearch = new HotSearch();
                newHotSearch.setHotTitle(hotTitle);
                newHotSearch.setClickNum(1);
                hotSearchRepository.save(newHotSearch);
            } else {
                //40%以上就将与该词条相似度最高的词条+1；
                System.out.println("新词条与数据库词条最大相似度大于0.4，词条点击量+1");
                Optional<HotSearch> optional = hotSearchRepository.findById(maxRatio.get().getKey());
                Integer clickNum = 0;
                if (!optional.equals(null)) {
                    clickNum = optional.get().getClickNum()+1;
                }
                Integer success = hotSearchRepository.updateHotSearch(maxRatio.get().getKey(), clickNum);
                System.out.println("success:"+success);
            }
        }
    }

    public List<String> hotTopFive() {
        List<HotSearch> list = hotSearchRepository.findAll();
        Map<Integer, Integer> clickNumMap = new HashMap<>();
        list.forEach(f ->{
            clickNumMap.put(f.getHotSearchId(), f.getClickNum());
        });
        //找出前五个最大点击量词条id
        List<Integer> topFiveIdList = new ArrayList<>();
        for (int i = 0; i < 5; i ++) {
            Optional<Map.Entry<Integer, Integer>> maxClick = clickNumMap.entrySet()
                    .stream()
                    .max(Map.Entry.comparingByValue());
            Integer key = maxClick.get().getKey();//得到点击量最大的词条id
            topFiveIdList.add(key);//将这个词条的id存起来
            clickNumMap.remove(key);//从map中删除这一词条，为了找出前5条
        }
        //获取前五个最大点击量词条的内容
        List<String> topFiveTitleList = new ArrayList<>();
        topFiveIdList.forEach(f ->{
            Optional<HotSearch> optional = hotSearchRepository.findById(f);
            topFiveTitleList.add(optional.get().getHotTitle());
        });
        return topFiveTitleList;
    }
}
